package com.honey.chat.controller;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.simp.SimpMessageSendingOperations;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.honey.chat.entity.Message;
import com.honey.chat.utils.R;

/**
 * 
 * @author honey
 * @version 创建时间：2020年12月6日 下午3:36:31
 */
@Controller
@RequestMapping("/chat")
public class ChatController {

	/** 消息发送工具对象 */
    @Autowired(required=false)
    private SimpMessageSendingOperations simpMessageSendingOperations;
    
	@GetMapping("/joinroom")
	public String chat() {
		
		return "/join";
	}
	
	@PostMapping("/joinroom")
	public String chat(Message message, ModelMap mmap,HttpServletRequest request) {
		mmap.put("roomnum", message.getRoomNum());
		mmap.put("username", message.getUserName());
		HttpSession session = request.getSession();
		session.setAttribute(message.getUserName(), message);
		return "/index";
	}
	
    @PostMapping("/sendmsg")
    @ResponseBody
    public R sendmsg(Message msg) {
    	msg.setSendTime(new Date());
    	System.out.println(msg);
    	simpMessageSendingOperations.convertAndSend(msg.getSendUrl(), msg);
    	return R.ok("发送成功");
    }
}
